package com.starsoft.frame.base.web;

import java.util.Map;

import javax.servlet.ServletRequest;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starsoft.frame.base.bean.BaseJob;
import com.starsoft.frame.base.bean.BaseJobLog;
import com.starsoft.frame.base.job.JobRegister;
import com.starsoft.frame.base.service.JobLogService;
import com.starsoft.frame.base.service.JobService;
import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.core.web.WebUtil;


@RestController
@RequestMapping(value = "/base/set")
public class JobController {
	@Autowired
	JobLogService logService;
	
	@Autowired
	JobService jobService;
	
	@Autowired
	JobRegister jobRegister;
	
	@RequestMapping(value = "/joblog")
	public Map<String, Object> jobLogPage(ServletRequest request) {
		PageObject po = WebUtil.getPageObject(request);
		return logService.pageQuery(po);
	}

	@RequestMapping(value = "/joblogdelete")
	public Map<String, Object> jobLogPage(BaseJobLog jobLog) {
		logService.delete(jobLog);
		return WebUtil.sucesseModel("删除成功");
	}
	
	@RequestMapping(value = "/jobpage")
	public Map<String, Object> jobPage(ServletRequest request) {
		PageObject po = WebUtil.getPageObject(request);
		return jobService.pageQuery(po);
	}
	
	@RequestMapping(value = "/jobsave")
	public Map<String, Object> jobSave(ServletRequest request,BaseJob job) throws ClassNotFoundException, SchedulerException {
		jobService.save(job);
		jobRegister.updateJob(job);
		return WebUtil.sucesseModel("保存成功");
	}
	
	@RequestMapping(value = "/jobdelete")
	public Map<String, Object> jobDelete(ServletRequest request, BaseJob job) throws SchedulerException {
		jobRegister.deleteJob(job);
		jobService.delete(job);
		return WebUtil.sucesseModel("删除成功");
	}
	
	@RequestMapping(value = "/jobtrigger")
	public Map<String, Object> jobTrigger(ServletRequest request, BaseJob job) throws SchedulerException {
		jobRegister.triggerOneTimeJob(job);
		return WebUtil.sucesseModel("触发成功");
	}
}
